﻿//创建时间: 2010-12-15
//创建作者: 李 城
//功能说明: cms外部页面访问基类 逻辑处理继承的基类
//最后修改: 2010-12-15
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;


using TRPOP.CMS.Model;
using TRPOP.CMS.Business;
using TRPOP.CMS.Plugin;
using TRPOP.CMS.Log;
using TRPOP.Common;
using TRPOP.Cache;

namespace TRPOP.CMS.Web.ExtraBus
{
    /// <summary>
    /// 逻辑处理继承的基类
    /// </summary>
    public class BasePage : System.Web.UI.Page
    {

        #region 基本变量
        protected string mCurrentUrl = RequestHelper.GetCurrentUrl();
        protected string mCacheKey = CacheKeys.Get_Dynamic(RequestHelper.GetCurrentUrl());
        protected bool mBlnEnableCache = SYSConfig.Cache_Enable;
        protected const double mCacheSeconds = CacheKeys.DynamicTimeSecond;
        protected ICache mICache = ICacheAccess.GetInstance();
        #endregion

        #region 执行页面URL级别的缓存
        public bool ExecutePageURLCahche()
        {
            /*
             * 之前的代码，没必要 先 IsExist 再 read，直接read 即可 
             * 
            if (mBlnEnableCache && mICache.IsExist(mCacheKey))
            {
                object obj = mICache.Read(mCacheKey);
                if (obj is string)
                {
                    Response.Write(obj.ToString());
                    Response.End();
                    return true;
                }
            }
             */
            object objReadFromCache = mICache.Read(mCacheKey);
            if (mBlnEnableCache && objReadFromCache is string)
            {
                Response.Write(objReadFromCache.ToString());
                Response.End();
                return true;
            }
            return false;
        }
        #endregion

        #region 日志管理
        /// <summary>
        /// 错误日志记录
        /// </summary>
        /// <param name="e"></param>
        protected override void OnError(EventArgs e)
        {
            Exception ex = Server.GetLastError();
            TRPOP.CMS.Business.LogManage oLogManage = new TRPOP.CMS.Business.LogManage();
            TRPOP.CMS.Model.LogInfo oLogInfo = new TRPOP.CMS.Model.LogInfo();
            oLogInfo.AddTime = DateTime.Now;
            oLogInfo.AddUserId = 0;
            oLogInfo.Detail = ILogAccess.GetInstance().SysLog(ex, new object[] { ex.Message, ex.Source, ex.StackTrace });
            oLogInfo.Type = (int)LogKeys.SysLogType.PageError;
            oLogManage.Save(oLogInfo);
            base.OnError(e);
        }
        #endregion

        #region 页面传递参数
        /// <summary>
        /// 针对页面传递的值，保存到视图里
        /// </summary>
        /// <param name="ResquestStr"></param>
        /// <returns></returns>
        protected string InitRequestViewState(string ResquestStr)
        {
            string oResquestStr = RequestHelper.RequestString(ResquestStr);
            ViewState[ResquestStr] = oResquestStr;
            return ViewState[ResquestStr].ToString();
        }

        /// <summary>
        /// 获取页面视图
        /// </summary>
        /// <param name="ResquestStr"></param>
        /// <returns></returns>
        protected string GetRequestViewState(string ResquestStr)
        {
            if (ViewState[ResquestStr] != null)
            {
                return ViewState[ResquestStr].ToString();
            }
            else
                return "";
        }
        #endregion

        /// <summary>
        /// 子页面初始化
        /// </summary>
        protected virtual void SubPageInit()
        {

        }

    }
}
